package com.yzt.loginmodule.login;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.IService.message.IMessageService;
import com.yzt.zhuju.arouter.RouterAccount;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.config.AppConfigCacheManager;
import com.yzt.zhuju.event.main.LoginCloseEvent;
import com.yzt.zhuju.network.entity.sysConfig.SystemConfigBean;
import com.yzt.zhuju.utils.SpannableFactory;
import com.yzt.zhuju.widget.tuiview.TUIImageView;
import com.yzt.loginmodule.R;
import com.yzt.loginmodule.widget.LoginTextPaint;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 登录
 */

public class LoginActivity extends BaseActivity {

    /**
     * select
     * 0 默认  其他：用户手动退出登录
     */
    @Autowired
    int comeInType;

    @Autowired
    String content;

    @Autowired
    IMessageService messageService;

    private TUIImageView imgCheck;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void installViews() {

        //如果进入这个页面，必然要退出账号，防止有无法退出融云账号的情况
        messageService.logout();

        setFullScreen(true, R.color.transparent);
        AppConfigCacheManager.getInstance().isLoginOut = true;

        // 手机号登录
        findViewById(R.id.tv_phone_login).setOnClickListener(this);

        // 一键登录
        findViewById(R.id.tv_one_key).setOnClickListener(this);

        // 访客模式
        findViewById(R.id.tv_visit_mode).setOnClickListener(this);

        imgCheck = findViewById(R.id.img_check);

        // 按钮复选
        imgCheck.setOnClickListener(this);

        LoginTextPaint clickableSpan = new LoginTextPaint() {
            @Override
            public void onClick(@NonNull View widget) {
                RouterMain.goToWeb(getString(R.string.user_agreement_url));
            }
        };

        LoginTextPaint clickableSpan1 = new LoginTextPaint() {
            @Override
            public void onClick(@NonNull View widget) {
                RouterMain.goToWeb(getString(R.string.privacy_policy_url));
            }
        };

        LoginTextPaint clickableSpan2 = new LoginTextPaint() {
            @Override
            public void onClick(@NonNull View widget) {
                SystemConfigBean bean = AppConfigCacheManager.getInstance().getSystemConfig();
                if (bean.getYinsi() != null && !"".equals(bean.getYinsi())) {
                    RouterMain.goToWeb(bean.getYinsi() + "?fullScreen=inde_");
                    return;
                }
                RouterMain.goToWeb(getString(R.string.privacy_policy_url));
            }
        };

        TextView tvProtocol = findViewById(R.id.tv_protocol);
        tvProtocol.setMovementMethod(LinkMovementMethod.getInstance());
        tvProtocol.setText(SpannableFactory
                .create("我已阅读并同意")
                .append("《用户服务协议》")
                .click(clickableSpan)
                .append("和")
                .append("《隐私权政策》")
                .click(clickableSpan1)
                .append("和")
                .append("《中国移动认证服务条款》")
                .click(clickableSpan2)
                .append("并授权煮桔获得本机号码")
                .build());

        // 登录关闭
        observerRxBus(LoginCloseEvent.class, event -> finish());
    }


    private ObjectAnimator animation;

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
    }

    @Override
    protected void onClick(int clickId) {
        super.onClick(clickId);
        if (clickId == R.id.tv_one_key) {
            // 一键登录
        } else if (clickId == R.id.tv_visit_mode) {
            // 访客模式
        } else if (clickId == R.id.tv_phone_login) {
            // 密码登录
            RouterAccount.SMSLoginActivity(mActivity, "", "86", 1, true);
        } else if (clickId == R.id.img_check) {
            // 按钮复选
            imgCheck.setSelected(!imgCheck.isSelected());
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (animation != null) {
            animation.cancel();
            animation = null;
        }
    }
}